﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace VirtualDisk
{
    class CopyCommand : Command
    {
        private MyString[] arguments = null;

        protected override int run()
        {
            if (arguments == null || arguments.Length == 0)
            {
                return 1;
            }

            MyString des;
            MyString source;
            processArgument(out des, out source);
            if (!DiskTree.getInstance().isExist(des) || !DiskTree.getInstance().isADirctory(des))
            {
                return 2;
            }

            MyString name = DiskHelper.getFileName(source);
            if (source.IndexOfAny(new char[] { '?', '*' }) == -1)
            {
                //未含有通配符
                if (!File.Exists(source.ToString()))
                {
                    return 3;
                }
                //复制
                processCopy(source, des);

            }
            else
            {
               //含有通配符，按通配符取出所有文件
                List<MyString> filenames = getAllFileByWildcard(source);
                if (filenames.Count == 0)
                {
                    return 3;
                }
                foreach (MyString filename in filenames)
                {
                    processCopy(filename, des);
                }

            }
            return 0;
        }
        //复制，并在des中已存在同名文件时，提示是否覆盖
        private void processCopy(MyString filename, MyString des)
        {
            MyString realName = DiskHelper.getFileName(filename);
            MyString nameInDisk = des + new MyString("\\") + realName;
            if (DiskTree.getInstance().isExist(nameInDisk))
            {
                if (!DiskTree.getInstance().isADirctory(nameInDisk))
                {
                    MyString strtemp = null;
                    while (true)
                    {
                        Console.Write(realName.ToString() + "存在, 是否覆盖(Y/N)? ");
                        strtemp = new MyString(Console.ReadLine());
                        //覆盖
                        if (strtemp.ToLower() == new MyString("y"))
                        {
                            DiskTree.getInstance().writeFileData(nameInDisk, filename);
                            break;
                        }
                        //取消复制
                        else if (strtemp.ToLower() == new MyString("n"))
                        {
                            break;
                        }
                    }
                }
                else
                    Console.WriteLine("子目录或文件" + nameInDisk.ToString() + "已经存在！");
            }
            else
                DiskTree.getInstance().createNodeByCopy(des, filename);
            
        }

        private void processArgument(out MyString des, out MyString source)
        {
            des = DiskTree.getInstance().getPWDPath();
            source = arguments[0].Trim();
            if (arguments.Length == 2)
            {
                des = arguments[1].Trim();
            }
        }
        protected override void processError(int returnCode)
        {
            switch (returnCode)
            {
                case 1:
                    Console.WriteLine("命令语法不正确");
                    break;
                case 2:
                    Console.WriteLine("系统找不到指定的路径！" + arguments[1].ToString());
                    break;
                case 3:
                    Console.WriteLine("系统找不到指定的文件！" + arguments[0].ToString());
                    break;
     
                default:
                    break;
            }
        }


        private List<MyString> getAllFileByWildcard(MyString path)
        {
            MyString diretoryPath = DiskHelper.getDirectoryName(path);
            List<MyString> realNames = new List<MyString>();
            try
            {
                string[] filenames = Directory.GetFiles(diretoryPath.ToString());
                MyString strtemp = DiskHelper.getCorrectWildcradName(path);
                Regex re = new Regex(strtemp.ToString(), RegexOptions.IgnoreCase);
                foreach (string filename in filenames)
                {
                    if (re.IsMatch(filename))
                    {
                        realNames.Add(new MyString(filename));
                    }
                }
            }
            catch (Exception e)
            {
            }
            return realNames;

        }

        public CopyCommand(MyString[] argu)
        {
            if (argu != null)
            {
                arguments = new MyString[argu.Length];
                for (int i = 0; i < arguments.Length; i++)
                {
                    arguments[i] = argu[i];
                }
            }
        }
    }
}
